package com.uni.bgController;


import com.uni.common.util.PageBean;
import com.uni.common.util.ReturnData;
import com.uni.entity.OverSeasInfo;
import com.uni.service.impl.OverSeasInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("overSeasInfo")
public class OverSeasInfoController {

    @Autowired
    private OverSeasInfoService overSeasInfoService;


    @RequestMapping("findList")
    public ReturnData findList(PageBean<OverSeasInfo> pageBean, String title){
        HashMap<String, Object> param = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(title)) {
            param.put("title", title);
        }
//        if(StringUtils.isNotBlank(hasSchool)) {
//            param.put("hasSchool", hasSchool);
//        }
        pageBean.setParam(param);
        List<OverSeasInfo> list = overSeasInfoService.findList(pageBean);
        int count = overSeasInfoService.countList(pageBean);
        pageBean.setContent(list);
        pageBean.setTotalCount(count);
        return new ReturnData().okBack(pageBean);
    }

    @PostMapping("add")
    public ReturnData add(OverSeasInfo overSeasInfo){
        ReturnData rd = new ReturnData();
        int count = overSeasInfoService.insert(overSeasInfo);
        if(count > 0) {
            return rd.okBack();
        }
        return rd.failBack();
    }

    @PostMapping("update")
    public ReturnData update(OverSeasInfo overSeasInfo){
        ReturnData rd = new ReturnData();
        int count = overSeasInfoService.update(overSeasInfo);
        if(count > 0) {
            return rd.okBack();
        }
        return rd.failBack();
    }

    @RequestMapping("delete")
    public ReturnData delete(String ids) {
        ReturnData rd = new ReturnData();
        if(StringUtils.isBlank(ids)) {
            return rd.failBack("请选择要删除的内容");
        }
        int rs =overSeasInfoService.delete(Arrays.asList(ids.split(",")));
        if (rs==0){
            return rd.failBack("没有删除任何内容");
        }
        return rd.okBack("共删除"+rs+"条记录");
    }
}
